package com.cq.hd.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.cq.hd.admin.annotation.RequiresPermissionsDesc;
import com.cq.hd.admin.biz.OrderBiz;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.order.api.TbOrderApi;
import com.cq.hd.order.api.dto.ActiveRankQueryDto;
import com.cq.hd.order.api.dto.ActiveVerifyRecordQueryDto;
import com.cq.hd.order.api.vo.ActiveVerifyMerchantVo;
import com.cq.hd.order.api.vo.AdminVerifyRecordPageVo;
import com.cq.hd.order.api.vo.AgentRankItemVo;
import com.cq.hd.product.api.TbActiveApi;
import com.cq.hd.product.api.dto.AdminActivePageDto;
import com.cq.hd.product.api.dto.AdminActiveRankPageDto;
import com.cq.hd.product.api.dto.AdminActiveVerifyRecordPageDto;
import com.cq.hd.product.api.vo.AdminActivePageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "活动管理相关接口")
@Slf4j
@RestController
@RequestMapping("/active")
public class TbActiveController {

    @Autowired
    private TbActiveApi activeApi;
    @Autowired
    private TbOrderApi orderApi;
    @Autowired
    private OrderBiz orderBiz;

    @ApiOperation(value = "活动列表")
    @ApiImplicitParam(name = "adminActivePageDto", value = "实体", paramType = "query", dataType = "AdminActivePageDto")
    @ApiResponse(response = AdminActivePageVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("active:active:list")
    @RequiresPermissionsDesc(menu = {"活动管理", "活动管理"}, button = "列表")
    @GetMapping("/page")
    public Result<Page<AdminActivePageVo>> pageAdminActive(AdminActivePageDto adminActivePageDto) {
        return activeApi.pageAdminActive(adminActivePageDto);
    }

    @ApiOperation(value = "活动详情")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "query", dataType = "Long")
    @ApiResponse(response = AdminActivePageVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("active:active:detail")
    @RequiresPermissionsDesc(menu = {"活动管理", "活动管理"}, button = "详情")
    @GetMapping("/details")
    public Result<AdminActivePageVo> adminDetails(Long id) {
        return activeApi.adminDetails(id);
    }

    @ApiOperation(value = "活动删除")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "query", dataType = "Long")
    @RequiresPermissions("active:active:delete")
    @RequiresPermissionsDesc(menu = {"活动管理", "活动删除"}, button = "详情")
    @PostMapping("/delete/{id}")
    public Result<Void> deleteActive(@PathVariable("id") Long id) {
        return activeApi.adminDelete(id);
    }

    @ApiOperation(value = "活动分销排行")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "query", dataType = "Long")
    @ApiResponse(response = AgentRankItemVo.class, code = 200, message = "接口返回对象参数")
    @RequiresPermissions("active:active:rank")
    @RequiresPermissionsDesc(menu = {"活动管理", "分销排行"}, button = "分销排行")
    @GetMapping("/pageRank")
    public Result<Page<AgentRankItemVo>> pageAgentRank(AdminActiveRankPageDto adminActiveRankPageDto) {
        return orderApi.pageAgentRank(BeanUtil.copyProperties(adminActiveRankPageDto, ActiveRankQueryDto.class));
    }

    @ApiOperation(value = "核销记录")
    @ApiImplicitParam(name = "adminActiveVerifyRecordPageDto", value = "实体", paramType = "query", dataType = "AdminActiveVerifyRecordPageDto")
    @ApiResponse(response = AgentRankItemVo.class, code = 200, message = "接口返回对象参数")
//    @RequiresPermissions("active:active:rank")
    @RequiresPermissionsDesc(menu = {"活动管理", "核销记录"}, button = "核销记录")
    @GetMapping("/pageVerifyRecord")
    public Result<Page<AdminVerifyRecordPageVo>> pageVerifyRecord(AdminActiveVerifyRecordPageDto adminActiveVerifyRecordPageDto) {
        return orderApi.pageAdminVerifyRecord(BeanUtil.copyProperties(adminActiveVerifyRecordPageDto, ActiveVerifyRecordQueryDto.class));
    }

    @ApiOperation(value = "活动核销商户")
    @ApiImplicitParam(name = "id", value = "活动id", paramType = "query", dataType = "Long")
    @ApiResponse(response = AgentRankItemVo.class, code = 200, message = "接口返回对象参数")
//    @RequiresPermissions("active:active:rank")
    @RequiresPermissionsDesc(menu = {"活动管理", "活动核销商户"}, button = "活动核销商户")
    @GetMapping("/listVerifyMerchant")
    public Result<List<ActiveVerifyMerchantVo>> listVerifyMerchant(Long activeId) {
        return orderApi.listVerifyMerchant(activeId);
    }

    @ApiOperation(value = "导出核销记录")
//    @RequiresPermissions("active:active:rank")
    @RequiresPermissionsDesc(menu = {"活动管理", "核销记录"}, button = "导出")
    @GetMapping("/exportVerifyRecord")
    public void exportVerifyRecord(AdminActiveVerifyRecordPageDto adminActiveVerifyRecordPageDto, HttpServletResponse response) {
        orderBiz.exportActiveVerifyRecord(BeanUtil.copyProperties(adminActiveVerifyRecordPageDto, ActiveVerifyRecordQueryDto.class), response);
    }
}
